<?php

/*******************************************************************************
 * 
 * charset.lib.php
 * 
 *******************************************************************************
 * 
 * @author Stanislas PERRIN <stanislas.perrin@gmail.com>
 * @licence LGPL v. 3.0
 * @package charset
 * 
 *******************************************************************************
 * 
 * Purpose
 * -------
 * Charset manipulation library.
 * 
 * Note(s)
 * -------
 * => php version 5.2 min. -- 5.3 recommended
 * => Errors are thrown via the php exception system : do not forget 
 * to try and catch while using this lib !
 * 
 * Dependancies
 * ------------
 * -> debug.class.php
 * 
 ******************************************************************************/

# ------------------------------------------------------------------------------
# namespace
# ------------------------------------------------------------------------------
# namespace phutile\libs\charset; // uncomment only if php version >= 5.3

# ------------------------------------------------------------------------------
# Dependencies inclusions
# ------------------------------------------------------------------------------
// debug.class.php
if (!@include_once('./debug.class.php')) die("impossible to load mandatory file debug.class.php");

# ------------------------------------------------------------------------------
# Locale configuration
# ------------------------------------------------------------------------------
$locale = 'en_EN';

# ------------------------------------------------------------------------------
# Locale file inclusion
# ------------------------------------------------------------------------------
$localeFile = "./locales/$locale.lang.php";
if (!@include_once($localeFile)) throw new debug("impossible to load/include [$localFile]",11);

################################################################################

/**
 *
 * convertStringCharset()
 * 
 * @param
 * @return
 *
 */
function convertStringCharset($string=false,$charset='UTF-8')
{
	if (!$string) throw new debug($_ERROR_CODES[101]);

	// looking for the actual string charset
	$inputStringCharset = mb_detect_encoding($string);
	
	// compare with asked charset
	if ($inputStringCharset != $charset)
	{
		// convert if necessary
		$string = iconv($inputStringCharset,$charset,$string);
	}
	// return the (converted) string
	return $string;
}

?>
